mongoose = require 'mongoose'
Schema = mongoose.Schema
ObjectID = Schema.ObjectID

module.exports = {}

# Custom content

CustomVocab = new Schema {
  co: { type: String, required: true }
  tr: { type: String, required: true }
}

CustomGrammar = new Schema {
  sd: { type: String, required: true }
  us: { type: String, required: true }
  ex: { type: String, required: true }
  eg: [GrammarExample]
}

CustomActivity = new Schema {
  ty: { type: String, required: true, enum: ACTIVITY_TYPES }
  ti: { type: String, required: true }
  ds: String
  co: { type: String, required: true }
  qu: [String]
  an: [String]
}

# User

Progress = new Schema {
  # ID of vocab item
  id: { type: String, required: true, match: /[a-f0-9]{12}/ }
  # progress
  pr: Number
}

UserClass = new Schema {
  # language
  la: { type: String, required: true, enum: SUPPORTED_LANGUAGES }
  # class
  cl: { type: String, required: true }
  # progress
  pr: [Progress]
  # custom vocab
  cv: [CustomVocab]
  # custom grammar
  cg: [CustomGrammar]
  # custom activities
  ca: [CustomActivity]
  # points
  pt: Number
}

User = new Schema {
  # username
  un: { type: String, required: true }
  # real name
  rn: { type: String, required: true }
  # email
  #TODO: Add validator
  em: { type: String, required: true }
  # password hash
  pw: { type: String, required: true, match: /[a-f0-9]{24}/ }
  # teacher?
  tc: Boolean
  # class list (should match those in uc - TODO: Add validator)
  # format <language>_<class>
  cl: [String]
  # classes
  uc: [UserClass]
  # login token
  tk: String
}

module.exports.User = mongoose.model 'User', User

# School

SchoolLanguage = new Schema {
  la: { type: String, required: true }
  cl: [String]
}

School = new Schema {
  # school ID
  si: { type: String, required: true, match: /[a-z]{3}/ }
  # school name
  sn: { type: String, required: true }
  # school email
  #TODO: Add validator
  em: { type: String, required: true }
  # password
  pw: { type: String, required: true, match: /[a-f0-9]{24}/ }
  # credits (1 credit = 1 student)
  cr: { type: Number, default: 0 }
  # login token
  tk: String
  # languages
  sl: [SchoolLanguage]
  # student username pattern
  sp: String
  # teacher username pattern
  tp: String
  # student email suffix
  se: String
  # teacher email suffix
  te: String
}

module.exports.School = mongoose.model 'School', School

# Vocab

Vocab = new Schema {
  # language
  la: { type: String, required: true, enum: SUPPORTED_LANGUAGES }
  # class
  cl: { type: String, required: true }
  # content (in the language)
  co: { type: String, required: true }
  # translation
  tr: { type: String, required: true }
  # categories
  ca: [String]
}

module.exports.Vocab = mongoose.model 'Vocab', Vocab

# Grammar

GrammarExample = new Schema {
  # content (in language)
  co: { type: String, required: true }
  # translation
  tr: { type: String, required: true }
}

Grammar = new Schema {
  # language
  la: { type: String, required: true, enum: SUPPORTED_LANGUAGES }
  # class
  cl: { type: String, required: true }
  # short description
  sd: { type: String, required: true }
  # usage
  us: { type: String, required: true }
  # explanation
  ex: { type: String, required: true }
  # examples
  eg: [GrammarExample]
}

module.exports.Grammar = mongoose.model 'Grammar', Grammar

# Activities

Activity = new Schema {
  # language
  la: { type: String, required: true, enum: SUPPORTED_LANGUAGES }
  # class
  cl: { type: String, required: true }
  # type
  ty: { type: String, required: true, enum: ACTIVITY_TYPES }
  # title
  ti: { type: String, required: true }
  # description
  ds: { type: String, required: true }
  # content (sound URL for listening, passage for reading, blank for worksheet)
  co: { type: String, required: true }
  # questions
  qu: [String]
  # answers
  an: [String]
}

module.exports.Activity = mongoose.model 'Activity', Activity
